/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
#include"PublicMySql.h"
#include"PublicTool.h"
#include"PublicLog.h"
#include "Common.h"
int LoadConfigFile(){
	FILE*fp=fopen(GetConfigFileName(),"rb");
	char* buff=NULL;
	size_t bufflen=0;
	ssize_t readlen=0;
	char key[32];
	char value[128];
	MySQLConf mysqlconf[MYSQL_NUM];
	memset(mysqlconf,0,sizeof(MySQLConf)*MYSQL_NUM);

	if(fp==NULL){
		printf("打开配置文件失败:%s\n",GetConfigFileName());
		return -1;
	}
	while((readlen=getline(&buff,&bufflen,fp))!=-1){
		TRIMC(buff,(size_t*)&readlen);
		if(buff[0]=='#'||buff[0]=='\0'||buff[0]==';'||buff[0]=='\\'||buff[0]=='/')
			continue;
		GetKeyAndValue(key,value,buff);
		if(strcasecmp(key,"SERVER")==0){
			strcpy(mysqlconf[MYSQL_BASE].host,value);
		}else
		if(strcasecmp(key,"USER")==0){
			strcpy(mysqlconf[MYSQL_BASE].user,value);
		}else
		if(strcasecmp(key,"PASSWD")==0){
			strcpy(mysqlconf[MYSQL_BASE].passwd,value);
		}else
		if(strcasecmp(key,"DATABASE")==0){
			strcpy(mysqlconf[MYSQL_BASE].db,value);
		}else
		if(strcasecmp(key,"PORT")==0){
			mysqlconf[MYSQL_BASE].port=atoi(value);
		}else
		if(strcasecmp(key,"SERVERFLOW")==0){
			strcpy(mysqlconf[MYSQL_FLOW].host,value);
		}else
		if(strcasecmp(key,"USERFLOW")==0){
			strcpy(mysqlconf[MYSQL_FLOW].user,value);
		}else
		if(strcasecmp(key,"PASSWDFLOW")==0){
			strcpy(mysqlconf[MYSQL_FLOW].passwd,value);
		}else
		if(strcasecmp(key,"DATABASEFLOW")==0){
			strcpy(mysqlconf[MYSQL_FLOW].db,value);
		}else
		if(strcasecmp(key,"PORTFLOW")==0){
			mysqlconf[MYSQL_FLOW].port=atoi(value);
		}else
		if(strcasecmp(key,"RRDDATAPATH")==0){
			strcpy(g_sysconf.rrddatapath,value);
		}
	}
	fclose(fp);
	if(buff!=NULL){
		free(buff);
		buff=NULL;
	}
	if(CheckSQLConf(mysqlconf[MYSQL_BASE])==0){
		SetMySQLConf(mysqlconf[MYSQL_BASE],MYSQL_BASE,1);
	}else{
		WRONG("请配置主库(SERVER/USER/PASSWD/PORT/DATABASE)");
		return -1;
	}
	if(CheckSQLConf(mysqlconf[MYSQL_FLOW])==0){
		SetMySQLConf(mysqlconf[MYSQL_FLOW],MYSQL_FLOW,1);
	}else{
		WRONG("请配置带宽库(SERVERFLOW/USERFLOW/PASSWDFLOW/PORTFLOW/DATABASEFLOW)");
		return -1;
	}
	return 0;
}
void ShowConfig(){
	ShowMySQLConf();
	printf("线程数:%d\n",g_sysconf.threads);
	printf("RRD存储路径:%s\n",g_sysconf.rrddatapath);
	printf("分段存储:%d\n",g_sysconf.savetype);
}
